package cn.com.cc.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import cn.com.cc.log.Logger;

public class ZipUtils {
	
	private static 	byte[] buf = new byte[1024];
	
	/**
	 * 压缩文件
	 * 
	 * @param zipFilePath	压缩后文件完整路径---路径+压缩后文件名
	 * @param files			数组,存储多个文件
	 */
	public static void zipFiles(String zipFilePath, File[] files){
		try{
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File(zipFilePath)));
			for(int i=0; i<files.length; i++){
				zipFile(out, files[i], "");
			}
			out.close();
		}catch(Exception e){
			e.printStackTrace();
			Logger.error("###create zipfile failure--->>function :: ZipUtils.zipFiles(String zipFilePath, File[] files)");
		}
	}
	
	private static void zipFile(ZipOutputStream out, File file, String path){
		try{
			if(!file.exists()){
				Logger.error("###file not exist--->>function :: ZipUtils.zipFile(ZipOutputStream out, File file)");
				throw new FileNotFoundException("file not exist;file name = " + file.getName());
			}
			path = path.length() == 0 ? "" : path + File.separator;
			if(file.isDirectory()){			//要压缩的文件是文件夹
				File[] files = file.listFiles();
				for(int i=0; i<files.length; i++){
					zipFile(out, files[i], path + file.getName());
				}
			}else{
				out.putNextEntry(new ZipEntry(path + new String(file.getName().getBytes(), "UTF-8")));
				FileInputStream in = new FileInputStream(file);
				int len;
				while((len = in.read(buf)) > 0){
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 解压文件
	 * 
	 * @param zipFile			要解压的压缩文件
	 * @param dirPath			解压后存放路径
	 */
	public static void unZipFile(File zipFile, String dirPath){
		try{
			if(!zipFile.exists()){
				throw new FileNotFoundException("###file not exist--->>>function :: ZipUtils.unZipFile(File zipFile, String dirPath)");
			}
			if(zipFile.isDirectory()){
				throw new IllegalArgumentException("###file is directory,--->>>function :: ZipUtils.unZipFile(File zipFile, String dirPath)");
			}
			File dirFile = new File(dirPath);
			if(!dirFile.exists()){
				Logger.warn("resource dirtory not exists and now create it");
				dirFile.mkdirs();
			}
			ZipFile file = new ZipFile(zipFile);
			for(Enumeration<ZipEntry> enu = file.getEntries(); enu.hasMoreElements(); ){
				ZipEntry entry = enu.nextElement();
				InputStream in = file.getInputStream(entry);
				if(entry.isDirectory()){
					continue;
				}
				String zipEntryName = entry.getName();
				System.out.println(zipEntryName);
				File innerFile = getRealFile(dirPath, zipEntryName);
				if(!innerFile.exists()){
					innerFile.createNewFile();
				}
				OutputStream out = new FileOutputStream(innerFile);
				int len;
				while ( (len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				in.close();
				out.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 根据dirPath路径创建其所有的父目录
	 * 
	 * @param dirPath
	 * @param entryName
	 * @return
	 */
	private static File getRealFile(String dirPath, String entryName){
		String[] dirs = entryName.split("/");
		File file = new File(dirPath);
		if(dirs.length > 1){			//拆分传来的文件路径
			for(int i=0; i<dirs.length -1; i++){
				file = new File(file, dirs[i]);
			}
		}
		if(!file.exists()){			//file为最小子文件的父目录
			file.mkdirs();
		}
		file = new File(file, dirs[dirs.length - 1]);
		return file;
	}
	
	public static void main(String[] args) {
//		File file = new File("F:\\修罗武神.txt");
//		File file1 = new File("F:\\JDK_API_1_6_zh_CN.CHM");
//		File file2 = new File("D:\\doc2help");
//		ZipUtils.zipFiles("d:\\大.zip", new File[]{file, file1, file2});
		
		unZipFile(new File("d:\\大.zip"), "d:\\ziptest");
//		if(file2.isDirectory()){
//			System.out.println("aa");
//		}
		
	}

}
